using Senparc.Weixin.Entities;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Senparc.Weixin.MP.AdvancedAPIs
{
	public static class CustomApi
	{
		private const string URL_FORMAT = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}";
		public static WxJsonResult SendText(string accessTokenOrAppId, string openId, string content, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				var data = new
				{
					touser = openId,
					msgtype = "text",
					text = new
					{
						content
					}
				};
				return CommonJsonSend.Send(accessToken, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult SendImage(string accessTokenOrAppId, string openId, string mediaId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				var data = new
				{
					touser = openId,
					msgtype = "image",
					image = new
					{
						media_id = mediaId
					}
				};
				return CommonJsonSend.Send(accessToken, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult SendVoice(string accessTokenOrAppId, string openId, string mediaId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				var data = new
				{
					touser = openId,
					msgtype = "voice",
					voice = new
					{
						media_id = mediaId
					}
				};
				return CommonJsonSend.Send(accessToken, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult SendVideo(string accessTokenOrAppId, string openId, string mediaId, string title, string description, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				var data = new
				{
					touser = openId,
					msgtype = "video",
					video = new
					{
						media_id = mediaId,
						title = title,
						description = description
					}
				};
				return CommonJsonSend.Send(accessToken, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult SendMusic(string accessTokenOrAppId, string openId, string title, string description, string musicUrl, string hqMusicUrl, string thumbMediaId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				var data = new
				{
					touser = openId,
					msgtype = "music",
					music = new
					{
						title = title,
						description = description,
						musicurl = musicUrl,
						hqmusicurl = hqMusicUrl,
						thumb_media_id = thumbMediaId
					}
				};
				return CommonJsonSend.Send(accessToken, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult SendNews(string accessTokenOrAppId, string openId, List<Article> articles, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				var data = new
				{
					touser = openId,
					msgtype = "news",
					news = new
					{
						articles = (
							from z in articles
							select new
							{
								title = z.Title,
								description = z.Description,
								url = z.Url,
								picurl = z.PicUrl
							}).ToList()
					}
				};
				return CommonJsonSend.Send(accessToken, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
			}, accessTokenOrAppId, true);
		}
	}
}
